package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.action.BaseCostroller;
import com.foreveross.common.CommonUtil;
import com.foreveross.service.HrNewsInforService;
import com.foreveross.vo.Express;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.NewsSearch;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

/**
 * 楼盘资讯 管理
 * 
 * @author zw
 * 
 */
@Controller
@RequestMapping(value = "/weixin/hrNewsInfoController")
public class HrNewsInfoController extends BaseCostroller {
	private Logger log = Logger.getLogger(HrNewsInfoController.class);

	@Resource
	HrNewsInforService hrNewsInforService;

	/**
	 * 获取资讯内容
	 * @param request
	 * @param response
	 * @param type 
	 * @param currentPage 当前页
	 * @param pageSize 天数
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrNewsInfoList")
	public void getHrNewsInfoList(HttpServletRequest request,
			HttpServletResponse response, String type, String currentPage,
			String pageSize) {
		String weixinId = CommonUtil.getWeixinID(request);
		ResultVo resultVo = new ResultVo();
		try {
			PageVo<HrNewsInfo> pageVo = new PageVo<HrNewsInfo>();
			int page = currentPage==null ? 1:Integer.parseInt(currentPage);
			int pSize = pageSize==null ? 3: Integer.parseInt(pageSize);
			int total =(int)hrNewsInforService.getTypeNewsInfoListCount(type, weixinId);
			List<HrNewsInfo> hrNewInfoList = hrNewsInforService.getTypeNewsInfoList(type, weixinId, 
					page, pSize);
			pageVo.settotal(total);
			pageVo.setCurrentPage(page);
			pageVo.setPageSize(pSize);
			pageVo.setData(hrNewInfoList);
			
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			if (pageVo.getData() == null || pageVo.getData().isEmpty()) {
				resultVo.setMessage("获取资讯列表为空！");
			}
			log.info("获取资讯列表成功");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("获取资讯列表失败");
			log.error("获取资讯列表失败!", e);
		}
		List list = Arrays.asList("type", "houseId", "sysUserByCreateId",
				"sysUserByPublisherId","newsHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}

	/**
	 * 搜索资讯内容列表
	 * 
	 * @param request
	 * @param response
	 * @param searchKey
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/searchHrNews")
	public void searchHrNews(HttpServletRequest request,
			HttpServletResponse response, String searchKey, String type,
			String currentPage, String pageSize) {
		ResultVo resultVo = new ResultVo();
		String weixinId = CommonUtil.getWeixinID(request);
		try {
			hrNewsInforService.searchHrNews(weixinId, searchKey, type,
					currentPage, pageSize, resultVo);
			log.info("搜索资讯列表 成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("搜索资讯列表失败");
			log.error("搜索资讯列表失败!", e);
		}
		List list = Arrays.asList("type", "houseId", "sysUserByCreateId",
				"sysUserByPublisherId","newsHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}

	/**
	 * 查看楼盘资讯详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findHrNewsInfo")
	public void findHrNewsInfo(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrNewsInfo hrNewsInfo = (HrNewsInfo) this.findObject(
					HrNewsInfo.class, id);
			resultVo.setSuccess(true);
			resultVo.setData(hrNewsInfo);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("楼盘资讯 查看失败");
			log.error("楼盘资讯详情  查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 搜索热词列表
	 * 
	 * @param request
	 * @param response
	 * @param searchKey
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/searchHotWord")
	public void searchHotWord(HttpServletRequest request,
			HttpServletResponse response, String searchKey, String pageSize,
			String type) {
		ResultVo resultVo = new ResultVo();
		try {
			List<NewsSearch> list = hrNewsInforService.findNewsSearchList(type,
					pageSize);
			Map<String, Object> map = new HashMap();
			if (list != null && !list.isEmpty()) {
				map.put("data", list);
				map.put("total", list.size()); 
				resultVo.setData(map);
			} else {
				resultVo.setData(new ArrayList());
				resultVo.setMessage("搜索热词列表为空！");
			}
			resultVo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("搜索热词列表失败");
			log.error("搜索热词列表失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
